package com.wslNB.goods.controller;

import com.wslNB.entity.PageResult;
import com.wslNB.entity.Result;
import com.wslNB.entity.StatusCode;
import com.wslNB.goods.pojo.Brand;
import com.wslNB.goods.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand")
@Api(tags = "品牌管理接口api")//标签专门用来标注类的
@CrossOrigin
public class BrandController {

    @Autowired
    private BrandService brandService;


    /**
     * 查询所有品牌
     *
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("查询列表接口")//标注方法
    public Result findAll() {

        try {
            List<Brand> list = brandService.findList();
            return new Result(true, StatusCode.OK, "查询所有品牌成功", list);
        } catch (Exception exception) {
            exception.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询所有品牌失败");
        }
    }


    /**
     * 根据ID查询品牌信息
     *
     * @param id
     * @return
     */
    @ApiOperation("查询列表接口")//标注方法
    @GetMapping("findById/{id}")
    public Result findById(@PathVariable Integer id) {

        try {
            Brand brandOne = brandService.findById(id);
            return new Result(true, StatusCode.OK, "查询成功", brandOne);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询品牌失败");
        }
    }


    /**
     * 新增品牌数据
     *
     * @param brand
     * @return
     */
    @ApiOperation("查询列表接口")//标注方法
    @PostMapping("/add")
    public Result addBrand(@RequestBody Brand brand) {
        try {
            brandService.add(brand);
            return new Result(true, StatusCode.OK, "新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "新增失败");
        }
    }


    /**
     * 修改品牌
     *
     * @param brand
     * @return
     */
    @ApiOperation("查询列表接口")//标注方法
    @PutMapping("/update")
    public Result updateBrand(@RequestBody Brand brand) {

        try {
            brandService.updateBrand(brand);
            return new Result(true, StatusCode.OK, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "修改失败");

        }
    }


    /**
     * 根据ID删除品牌
     *
     * @param id
     * @return
     */
    @ApiOperation("查询列表接口")//标注方法
    @DeleteMapping("/delete/{id}")
    public Result deleteBrand(@PathVariable Integer id) {

        try {
            brandService.deleteBrand(id);
            return new Result(true, StatusCode.OK, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "删除失败");
        }
    }


    /**
     * 根据条件查询
     *
     * @param brand
     * @return
     */
    @ApiOperation("查询列表接口")//标注方法
    @GetMapping("/findByNameAndLetter")
    public Result findByNameAndLetter(@RequestBody Brand brand) {
        try {
            List<Brand> byNameAndLetter = brandService.findByNameAndLetter(brand);
            return new Result(true, StatusCode.OK, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }


    //分页查询
    @GetMapping("/findPage/{currentPage}/{pageSize}")
    @ApiImplicitParams({//参数集合
            @ApiImplicitParam(name = "currentPage",value = "页码",defaultValue = "1",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页数量",defaultValue = "5",required = true)
    })
    public PageResult findPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize, @RequestBody Brand brand) {

        //pageResult
        PageResult page = brandService.findPage(currentPage, pageSize,brand);
        return page;

    }


    /**
     * 根据分类名称查询品牌列表
     * @param categoryName
     * @return
     */
    @GetMapping("/categoryName")
    public Result findListByCategoryName(String categoryName){

        try {
            List<Brand> listByCategoryName = brandService.findListByCategoryName(categoryName);
            return new Result(true,StatusCode.OK,"查询成功",listByCategoryName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
    }


}
